package com.sh.apiclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import com.sh.apiclientsdk.utils.SignUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 抽取ak  sk    封装生成签名的过程-
 * @author sh
 */
public class CommonApiClient {

    protected final String accessKey;
    protected final String secretKey;
    protected static final String GATEWAY_HOST ="http://localhost:18090";   //网关地址
    public CommonApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    /**
     * 封装用户传的参数  返回后 客户端加入请求头里
     * @param body
     * @return
     */
    protected static Map<String,String> getHeadMap(String body, String accessKey, String secretKey){
        //六个参数
        Map<String,String> headMap = new HashMap<>();
        headMap.put("accessKey",accessKey);
        headMap.put("body",body);
        headMap.put("sign", SignUtils.getSign(body,secretKey));
        headMap.put("nonce", RandomUtil.randomNumbers(5));  // 随机数解决重放
        //当下时间/1000，时间戳大概10位
        headMap.put("timestamp",String.valueOf(System.currentTimeMillis()/1000));
        return headMap;
    }

}
